package com.itheima.tlias.servies.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias.mapper.EmpMapper;
import com.itheima.tlias.pojo.Emp;
import com.itheima.tlias.pojo.PageBean;
import com.itheima.tlias.servies.EmpServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServicesImpl implements EmpServices {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean list(Integer page, Integer pagesize,String name, Short gender,
                         LocalDate begin,
                         LocalDate end) {
        PageHelper.startPage(page,pagesize);

        List<Emp> list = empMapper.list(name,gender, begin, end);
        Page<Emp> p = (Page<Emp>) list;
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void insert(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        Emp e = empMapper.getByUsernameAndPassword(emp);
        return e;
    }
}
